#pragma once

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <assert.h>
#include <netdb.h>
#include <string.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <map>
#include <mutex>

using namespace std;

#include "logcplus_base_writer.h"

namespace logcplus
{
    class socket_client
    {
    private:
        int m_port;
        int m_sockFd;
        struct sockaddr_in m_sockAddr;
        bool m_isHeader;
    public:
        socket_client();
        ~socket_client();

        void setHeader(bool on){ m_isHeader = on; };
        int connect(const char* psIPAddr,int port);
        int disConnect();

        int begin( const char* psCDC,const char* psLogSeqno,const char* psHostName);
        int write( const char* psCDC,const char* psLogSeqno,const char* psHostName,const char* psLogText);
        int commit(const char* psCDC,const char* psLogSeqno,const char* psHostName);
    private:
        int send_request(const char* psLogText);
        int recv_response(char* psLogText);	 
    };

    class logcplus_writer_remote_udp : public logcplus_base_writer
    {
    private:
        char m_logCdc[40];
        char m_hostName[30];
        string m_ipaddr;
        int m_port;
        socket_client m_client;
    public:
        logcplus_writer_remote_udp();
        ~logcplus_writer_remote_udp();

        int  LogInit();
        int  LogInit(const string& sLogName, int nLogLevel,const string& subNode = string());
        int  LogUInit();
        int  LogBegin(uint64_t logMsgId = 0);
        void LogCommit();
        void LogWrite(const char* File, const char* Function, int Line,  int Level, const char* LogMsg);
        void LogWrite(const char* sLogText,int nFlag=0);
        
        void Trace(  const char* File, const char* Function, int Line,  const char* Fmt, ...);
        void Debug(  const char* File, const char* Function, int Line,  const char* Fmt, ...);
        void Info(   const char* File, const char* Function, int Line,  const char* Fmt, ...);
        void Warn(   const char* File, const char* Function, int Line,  const char* Fmt, ...);
        void Error(  const char* File, const char* Function, int Line,  const char* Fmt, ...);
        void Fatal(  const char* File, const char* Function, int Line,  const char* Fmt, ...);

        void LogHex( const char* File, const char* Function, int Line,  int Level, const void* data, size_t dataSize );
        void Logger( const char* File, const char* Function, int Line,  int Level, const char* Fmt, ...);
        void WLog(   const char* File, const char* Function, int Line,  int Level, const char* sLogText);
    };    

}